package com.tongzhou.tongzhou.account.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IDefaultCallDAO;
import com.tongzhou.tongzhou.account.model.DefaultCall;
import com.tongzhou.tongzhou.account.model.SearchDefaultCallCondition;
import com.tongzhou.util.Util;

public class DefaultCallDAO implements IDefaultCallDAO{

	@Override
	public int addDefaultCall(DefaultCall defaultCall) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into default_call(server_call,offer_call,about_call) ");
		sql.append("values('"+Util.changeToObj(defaultCall.getServer_call())+"',");
		sql.append("'"+Util.changeToObj(defaultCall.getOffer_call())+"',");
		sql.append("'"+Util.changeToObj(defaultCall.getAbout_call())+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deleteDefaultCall(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from default_call where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public DefaultCall getDefaultCallById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,server_call,offer_call,about_call ");
		sql.append(" from default_call ");
		sql.append(" where id='"+id+"'");
		DefaultCall defaultCall=DBUtil.query(sql.toString(), new BeanHandler<DefaultCall>(DefaultCall.class));
		return defaultCall;
	}

	@Override
	public List<DefaultCall> listDefaultCall(
			SearchDefaultCallCondition condition) throws DBException {
		List<DefaultCall> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from default_call ");
		sql.append(" where 1=1 ");
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select id,server_call,offer_call,about_call ");
		sql.append(" from default_call ");
		sql.append(" where 1=1 ");
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<DefaultCall>(DefaultCall.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateDefaultCall(DefaultCall defaultCall) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update default_call set ");
		sql.append("server_call='"+Util.changeToObj(defaultCall.getServer_call())+"',");
		sql.append("offer_call='"+Util.changeToObj(defaultCall.getOffer_call())+"',");
		sql.append("about_call='"+Util.changeToObj(defaultCall.getAbout_call())+"' ");
		sql.append("where id='"+defaultCall.getId()+"'");
		DBUtil.update(sql.toString());
	}

}
